﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using NewRetail.Application.Components.Advertisement;
using NewRetail.Application.Components.Advertisement.Entity;
using NewRetail.Application.Components.Advertisement.Model;
using NewRetail.Application.Core;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace NewRetail.Api.Controllers
{
    [NewRetailRoute("advertisement")]
    public class AdvertisementController : BaseController<AdvertisementInfo, AdvertisementPagingDto>
    {
        private readonly IAdvertisementService advertisementService;
        public AdvertisementController(IAdvertisementService advertisementService)
        {
            this.advertisementService = advertisementService;
        }

        [Obsolete]
#pragma warning disable CS0809 // 过时成员重写未过时成员
        public override async Task<ResponseEntity<AdvertisementInfo>> Save([FromBody] AdvertisementInfo dto)
#pragma warning restore CS0809 // 过时成员重写未过时成员
        {
            return await Task.Run(() => new ResponseEntity<AdvertisementInfo>());
        }

        [Obsolete]
#pragma warning disable CS0809 // 过时成员重写未过时成员
        public override async Task<ResponseEntity<object>> SyncData([FromBody] AdvertisementInfo[] data)
#pragma warning restore CS0809 // 过时成员重写未过时成员
        {
            return await Task.Run(() => new ResponseEntity<object>());
        }

        [HttpPost("AddAdvertisementPics")]
        public async Task<ResponseEntity<IEnumerable<AdvertisementPicResultDto>>> AddAdvertisementPics([FromForm] AdvertisementPicAddDto inputDto)
        {
            var ret = new ResponseEntity<IEnumerable<AdvertisementPicResultDto>>();
            try
            {
                //检测附件大小，默认支持5M以下
                var largeAttachments = inputDto.attachments?.Where(_ => _.Length > 50 * 1024 * 1024);
                if (largeAttachments?.Count() > 0)
                {
                    ret.SetError($"附件{string.Join(",", largeAttachments.Select(_ => _.FileName))}过大,建议不要超过5M！");
                    return ret;
                }
                //定义图片数组后缀格式
                string[] LimitPictureType = { ".GIF", ".JPG", ".JPEG", ".PNG", ".BMP" };

                var attachmentFileNames = inputDto.attachments?.Select(x => x.FileName);
                foreach (var item in attachmentFileNames)
                {
                    //获取图片后缀是否存在数组中
                    string currentPictureExtension = Path.GetExtension(item).ToUpper();
                    if (!LimitPictureType.Contains(currentPictureExtension))
                    {
                        ret.SetError($"文件格式不正确，只允许(.gif) | (.jpg) | (.bmp) | (.jpeg) | (.png)！");
                        return ret;
                    }
                }

                var result = await this.advertisementService.AddAdvertisementPics(inputDto);
                if (!string.IsNullOrEmpty(result.Item2))
                    ret.SetError(result.Item2);
                else
                    ret.SetData(result.Item1);
            }
            catch (Exception e)
            {
                ret.SetError(e.Message);
            }
            return ret;
        }
    }
}
